package cn.edu.seu.herald.informationplatform.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.seu.herald.informationplatform.bean.league.Activity;
import cn.edu.seu.herald.informationplatform.dao.cookie.LeagueUserSessionCookieAccess;
import cn.edu.seu.herald.informationplatform.dao.cookie.SessionIdNotFound;
import cn.edu.seu.herald.informationplatform.dao.database.ActivityDao;
import cn.edu.seu.herald.informationplatform.filter.ActivityRequired;

public class ActivityEdit extends HttpServlet {

	private static final boolean SUCCESS = true;

	private static final boolean FAILURE = false;

	private static final String SUCCESS_MESSAGE = "操作成功";

	private static final String FAILURE_MESSAGE = "操作失败";

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response)
					throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		try {
			ActivityDao activityAccess = ActivityDao.getDao();
			
			int activityId = Integer.valueOf(request.getParameter(
					ActivityRequired.PARAM_NAME_OF_ACTIVITY_ID));
			Activity previousActivity = activityAccess.getActivityById(
					activityId);
			
			String classId = request.getParameter("classId");
			String name = request.getParameter("name");
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			String information = request.getParameter("information");
			String postAddress = request.getParameter("postAddress");
			String contact = request.getParameter("contact");
			String organizer = request.getParameter("organizer");
			
			Activity activity = new Activity();
			activity.setClassId((classId == null)
					? previousActivity.getClassId()
							: Integer.valueOf(classId));
			activity.setName((name == null)
					? previousActivity.getName()
							: name);
			activity.setStartTime((startTime == null)
					? previousActivity.getStartTime()
							: startTime);
			activity.setEndTime((endTime == null)
					? previousActivity.getEndTime()
							: endTime);
			activity.setInformation((information == null)
					? previousActivity.getInformation() : information);
			activity.setPostAddress((postAddress == null)
					? previousActivity.getPostAddress()
							: postAddress);
			activity.setContact((contact == null)
					? previousActivity.getContact()
							: contact);
			activity.setOrganizer((organizer == null)
					? previousActivity.getOrganizer()
							: organizer);
			activity.setApplicantCount(0);
			activity.setReleaseTime(new java.sql.Date(
					new java.util.Date().getTime()));
			
			activityAccess.updateActivity(activityId, activity);
			LeagueUserSessionCookieAccess luSessionCookieAccess =new LeagueUserSessionCookieAccess(request);
			response.sendRedirect("./activities.jsp?league="+luSessionCookieAccess.getLeagueId());
		} catch (SQLException e) {
			printResult(response, FAILURE);
			return;
		} catch (NumberFormatException e) {
			printResult(response, FAILURE);
			return;
		} catch (SessionIdNotFound e) {
			printResult(response, FAILURE);
			return;
		}
		printResult(response, SUCCESS);
	}
	
	private void printResult(HttpServletResponse response,
			boolean success) throws IOException {
		response.getWriter().println((success) ?
				SUCCESS_MESSAGE : FAILURE_MESSAGE);
	}

}
